package cn.com.activemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicSubscriber;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;

/**
 * Hello world!
 *
 */
public class JMSSubscriber 
{
	private static final String USERNAME = ActiveMQConnectionFactory.DEFAULT_USER; 
	private static final String PASSWORD = ActiveMQConnectionFactory.DEFAULT_PASSWORD; 
	private static final String BROKERURL = ActiveMQConnectionFactory.DEFAULT_BROKER_URL; 
	
	public static void main( String[] args ) throws JMSException
	{
		ConnectionFactory connectionFactory; // 消息中间件的连接工厂
		Connection connection = null; // 消息连接
		Session session = null; // 创建会话
		
		connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKERURL);
		try {
			connection = connectionFactory.createConnection();
			connection.setClientID("1");  
			// 要先启动链接才能创建会话
			connection.start();
			// 创建session，进行消息的发送
			session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
			
			// 创建消息队列
			Topic topic = session.createTopic("topic1");
			
			// 创建消息生产者，非持久订阅方式
//			MessageConsumer messageConsumer = session.createConsumer(topic);
//			messageConsumer.setMessageListener(new MyListener());
			
			// 持久订阅方式，不会漏掉信息
			TopicSubscriber subs = session.createDurableSubscriber(topic,"1");
			while(true){
				subs.setMessageListener(new MyListener());
			}
			
//			while(true) {
//				Message message = subs.receive();
//				if (message instanceof TextMessage) {
//					System.out.println("接收到文本信息："+((TextMessage)message).getText());
//				} else if(message instanceof MapMessage){
//					MapMessage map = (MapMessage)message;
//					String name = map.getString("name");
//					int age = map.getInt("age");
//					System.out.println("name:"+name+",age:"+age);
//				}
//			}
		} catch (JMSException e) {
			e.printStackTrace();
		} finally {
			if (connection != null) {
				connection.close();
			}
			if (session != null) {
				session.close();
			}
		}
	}
	
	public static class MyListener implements MessageListener {
		public void onMessage(Message message) {
			try {
				if (message instanceof TextMessage) {
					System.out.println("接收到文本信息："+((TextMessage)message).getText());
				} else if(message instanceof MapMessage){
					MapMessage map = (MapMessage)message;
					String name = map.getString("name");
					int age = map.getInt("age");
					System.out.println("name:"+name+",age:"+age);
				}
				message.acknowledge();
			} catch (Exception e) {
			}
			
		}
	}
}
